package com.choosefine.it.common.conf;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import com.choosefine.it.base.HttpServiceBean;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Created by Administrator on 2016/11/10.
 */
@Configuration
public class BeanConfig {
	private final Logger logger = LoggerFactory.getLogger("【yaml文件的配置读取】");
	private Map<String, Map> configAsMap = new HashMap<String, Map>();
	private Map<String, Properties> configAsProperties = new HashMap<String, Properties>();

	@Bean
	public RedisTemplate<String, String> redisTemplate(
			RedisConnectionFactory redisConnectionFactory) {
		StringRedisTemplate template = new StringRedisTemplate(
				redisConnectionFactory);
		setSerializer(template); // 设置序列化工具
		template.afterPropertiesSet();
		return template;
	}

	private void setSerializer(StringRedisTemplate template) {
		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(
				Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		template.setValueSerializer(jackson2JsonRedisSerializer);
	}

	@Bean
	public Properties yaml() {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
		Properties resultProperties = null;
		try {
			logger.info("从<配置文件>中<>读取");
			yaml.setResources(resolver
					.getResources("classpath:config/**/*.yml"));
			resultProperties = yaml.getObject();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("无法读取 yaml 文件", e);
			resultProperties = null;
		}
		return resultProperties;
	}

	/**
	 * 从yaml中读取配置
	 * 
	 * @param yamlSource
	 * @return
	 * @author guwenwei
	 * @date 2016/11/29 5:49
	 */
	public Map<String, Object> yaml2Map(String yamlSource) {
		Map resultMap = null;
		try {
			if (configAsMap.containsKey(yamlSource.trim())) {
				logger.info("直接从内存中读取");
				resultMap = configAsMap.get(yamlSource.trim());
			} else {
				logger.info("从配置文件中读取");
				YamlMapFactoryBean yaml = new YamlMapFactoryBean();
				yaml.setResources(new ClassPathResource(yamlSource));
				resultMap = yaml.getObject();
				configAsMap.put(yamlSource, resultMap);
			}
		} catch (Exception e) {
			logger.error("无法读取 yaml 文件", e);
			resultMap = null;
		}
		return resultMap;
	}

	/**
	 * 从property中读取配置
	 * 
	 * @param yamlSource
	 * @return
	 * @author guwenwei
	 * @date 2016/11/29 5:49
	 */
	public Properties yaml2Properties(String yamlSource) {
		Properties resultProperties = null;
		try {
			if (configAsProperties.containsKey(yamlSource.trim())) {
				logger.info("直接从<内存>中<>读取");
				resultProperties = configAsProperties.get(yamlSource.trim());
			} else {
				logger.info("从<配置文件>中<>读取");
				YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
				yaml.setResources(new ClassPathResource(yamlSource));
				resultProperties = yaml.getObject();
				configAsProperties.put(yamlSource, resultProperties);
			}
		} catch (Exception e) {
			logger.error("无法读取 yaml 文件", e);
			resultProperties = null;
		}
		return resultProperties;
	}

	/**
	 * 将yml转换为properties
	 * 
	 * @param yamlSource
	 * @param key
	 * @return
	 * @author guwenwei
	 * @date 2016/11/29 5:49
	 */
	public Properties yaml2Properties(String yamlSource, String key) {
		Properties resultProperties = new Properties();
		Map map = yaml2Properties(yamlSource);
		for (Object propertyKey : map.keySet()) {
			if ((propertyKey + "").startsWith(key + ".")) {
				resultProperties.put(propertyKey, map.get(propertyKey));
			}
		}
		return resultProperties;
	}

	@Bean
	public HttpServiceBean httpServiceBean() {
		HttpServiceBean bean = new HttpServiceBean();
		return bean;
	}
}
